/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.sample.network.swing;

import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.beans.EventHandler;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JOptionPane;

import nemos.swing.IActionProvider;
import nemos.util.log.NemosLogger;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;

/**
 * SampleActions : The sample actions provider.
 * 
 * @author Romain DUBOIS
 */
@Service(IActionProvider.class)
@Component
public class SampleActions extends WindowAdapter implements IActionProvider {

    /** The exit action name */
    private static final String EXIT_NAME = "sample.Exit"; //$NON-NLS-1$

    /** The actions */
    private final Map<String, Action> mActions;
    /** The logger */
    private NemosLogger mLogger;

    /** The bundle context */
    private BundleContext mContext;

    /**
     * Constructor.
     */
    public SampleActions() {
        super();

        mActions = new HashMap<String, Action>();
    }

    /**
     * Exit the system.
     */
    public void exit() {

        // Collect labels
        final String lTitle = mLogger.getTranslator().getLabel(
                NetworkMessage.CONFIRM_EXIT_TITLE);
        final String lText = mLogger.getTranslator().getLabel(NetworkMessage.CONFIRM_EXIT_TEXT);

        // Ask user to confirm exit
        final int lResult = JOptionPane.showConfirmDialog(null, lText, lTitle,
                JOptionPane.YES_NO_OPTION);

        // Exit if confirmed
        if (lResult == JOptionPane.YES_OPTION) {
            try {
                mContext.getBundle(0).stop();
            } catch (final BundleException e) {
                mLogger.error(NetworkMessage.STOP_ERROR, e);
            }
        }
    }

    /**
     * Start the component.
     * 
     * @param pContext
     *            The bundle context
     */
    @Activate
    public void start(final BundleContext pContext) {
        mContext = pContext;
        mLogger = new NemosLogger(SampleActions.class);

        mActions.put(EXIT_NAME, new AbstractAction() {

            /** Serial version */
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(@SuppressWarnings("unused") final ActionEvent e) {
                new Thread(EventHandler.create(Runnable.class, SampleActions.this, "exit")).start(); //$NON-NLS-1$
            }
        });
    }

    /**
     * Stop the component.
     */
    @Deactivate
    protected void stop() {
        for (final Action lAction : mActions.values()) {
            lAction.setEnabled(false);
        }
        mActions.clear();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.IActionProvider#getAction(java.lang.String)
     */
    @Override
    public Action getAction(final String pActionId) {
        return mActions.get(pActionId);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.swing.IActionProvider#getAvailableActionIds()
     */
    @Override
    public Collection<String> getAvailableActionIds() {
        return Collections.unmodifiableCollection(mActions.keySet());
    }
}
